//
//  UILabel.swift
//  Test
//
//  Created by ZWJY on 2021/8/20.
//

import Foundation
import UIKit

// MARK: - 快速创建UILabel
public extension UILabel {

    convenience init(text: String? = nil, textColor: UIColor? = nil, font: UIFont) {
        self.init()
        self.font = font
        self.text = text
        self.textColor = textColor
    }
    
    func pg_sizeToFit() -> Self {
        self.sizeToFit()
        return self
    }
    
    func pg_text(_ title: String?) -> Self {
        self.text = title
        return self
    }
    
    func pg_font(_ font: UIFont) -> Self {
        self.font = font
        return self
    }

    func pg_lineBreakMode(_ mode: NSLineBreakMode) -> Self {
        self.lineBreakMode = .byTruncatingMiddle
        return self
    }
    
    func pg_textColor(_ color: UIColor, _ alpha: CGFloat = 1) -> Self {
        self.textColor = color.withAlphaComponent(alpha)
        return self
    }
    
    func pg_textAlignment(_ textAlignment: NSTextAlignment) -> Self {
        self.textAlignment = textAlignment
        return self
    }
    
    func pg_numberOfLines(_ numberOfLines: Int) -> Self {
        self.numberOfLines = numberOfLines
        return self
    }

    ///  horizontal 横向抗压缩，vertical 竖向抗压缩
    /// - Parameter axis: 压缩方向
    func pg_setContentCompressionResistancePriority(_ axis: NSLayoutConstraint.Axis) -> Self {
        self.setContentCompressionResistancePriority(.required, for: axis)
        return self
    }

    ///  horizontal 横向抗拉伸，vertical 竖向抗拉伸
    /// - Parameter axis: 压缩方向
    func pg_setContentHuggingPriority(axis: NSLayoutConstraint.Axis) -> Self {
        self.setContentHuggingPriority(.required, for: axis)
        return self
    }
}
